const perfix = "http://8.142.22.78:3000/api/";

const http = {
  get(url, params) {
    if (params) {
      let arr = [];
      url += "?";
      for (const key in params) {
        arr.push(key + "=" + params[key]);
      }
      url += arr.join("&");
    }
    return new Promise((resolve, reject) => {
      return fetch(perfix + url)
        .then((response) => response.json())
        .then((res) => {
          if (res.status === "0") {
            resolve(res);
          } else {
            reject(res.msg);
          }
        });
    });
  },
  post(url, params) {
    return new Promise((resolve, reject) => {
      return fetch(perfix + url, {
        body: JSON.stringify(params), //这里是要传递的参数
        headers: {
          "content-type": "application/json",
        },
        method: "POST",
      })
        .then((response) => response.json())
        .then((res) => {
          if (res.status === 0) {
            resolve(res);
          } else {
            reject(res.msg);
          }
        });
    });
  },
};

export default http;
